import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-07-27
 * Time:3:15
 */
@WebServlet(name = "RedirectServlet", urlPatterns = "/redirectServlet")
public class RedirectServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*
        重定向静态资源
        方法一: resp.sendRedirect("http://localhost:8080/工程路径/资源路径/资源");
        方法二: resp.sendRedirect("/工程路径/资源路径/资源");
        方法三: resp.sendRedirect("相对资源路径/资源")
        方法四: String contextPath = getServletContext().getContextPath();     contextPath = "/工程路径"
               resp.sendRedirect(contextPath + "/资源路径/资源");
        方法四最优
         */
        String contextPath = getServletContext().getContextPath();
        resp.sendRedirect(contextPath + "/b1/b2/base.html");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
